class Solution
{
public:
    int minimumAddedInteger(vector<int> &nums1, vector<int> &nums2)
    {
        sort(nums1.begin(), nums1.end());
        sort(nums2.begin(), nums2.end());
        int result = INT_MAX;
        for (int i = 0; i < nums1.size(); ++i)
        {
            for (int j = i + 1; j < nums1.size(); ++j)
            {
                int i1 = 0;
                int i2 = 0;
                int diff = 0;
                bool flag = true;
                while (i2 < nums2.size())
                {
                    if (i1 == i || i1 == j)
                    {
                        i1++;
                        continue;
                    }
                    if (i2 == 0)
                    {
                        diff = nums2[i2] - nums1[i1];
                    }
                    else
                    {
                        if (nums2[i2] - nums1[i1] != diff)
                        {
                            flag = false;
                            break;
                        }
                    }
                    i1++;
                    i2++;
                }
                if (flag)
                {
                    result = min(result, diff);
                }
            }
        }
        return result;
    }
};